package com.ywjh.inter.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import com.ywjh.inter.entity.UserPlanTimeEntity;

public class UserBGluTimeDAO {

	public void deleteByUserId(Connection conn, int userId) throws SQLException {
		PreparedStatement ps = conn.prepareStatement("delete from user_plan_time where user_id=?");
		ps.setInt(1, userId);
		ps.executeUpdate();
	}

	public boolean set(Connection conn, List<UserPlanTimeEntity> list) throws SQLException {
		boolean b=false;
		PreparedStatement ps = conn.prepareStatement("insert into user_plan_time(plan_id,start_hh,end_hh,user_id,add_time) values(?,?,?,?,?)");
		for(UserPlanTimeEntity e:list) {
			ps.setInt(1, e.getPlan_id());
			ps.setString(2, e.getStart_hh());
			ps.setString(3, e.getEnd_hh());
			ps.setInt(4, e.getUser_id());
			ps.setTimestamp(5, e.getAdd_time());
			ps.addBatch();
		}
		int[] i=ps.executeBatch();
		if(i.length>0) {
			b=true;
		}
		return b;
	}

	public List<UserPlanTimeEntity> listByUserId(Connection conn, int userId) throws SQLException {
		List<UserPlanTimeEntity> list=new LinkedList<UserPlanTimeEntity>();
		PreparedStatement ps = conn.prepareStatement("select * from user_plan_time where user_id=?");
		ps.setInt(1, userId);
		ResultSet rs=ps.executeQuery();
		while(rs.next()) {
			UserPlanTimeEntity e=fill(rs);
			list.add(e);
		}
		return list;
	}

	private UserPlanTimeEntity fill(ResultSet rs) throws SQLException {
		UserPlanTimeEntity e=new UserPlanTimeEntity();
		e.setId(rs.getInt("id"));
		e.setAdd_time(rs.getTimestamp("add_time"));
		e.setEnd_hh(rs.getString("end_hh"));
		e.setStart_hh(rs.getString("start_hh"));
		e.setPlan_id(rs.getInt("plan_id"));
		e.setUser_id(rs.getInt("user_id"));
		return e;
	}

}
